/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.filter;

import static com.gotobject.filter.FilterQ.from;
import static com.gotobject.filter.common.Predicates.eq;
import static com.gotobject.filter.common.Iterables.asList;
import static com.gotobject.filter.common.Orderings.desc;
import static com.gotobject.filter.common.Orderings.length;
import static com.gotobject.filter.common.Orderings.multipleOf;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class EvaluateThenSortThenEvaluateTest {
    @Test
    public void orderBy_Length() throws Exception {
        final List<String> names = Arrays.asList("John", "Mary", "Roberto", "David", "Fernando");
        final List<String> orderedByLengthNames = asList(from(names).orderBy(length()).select());
        assertEquals("same list", "[John, Mary, David, Roberto, Fernando]", orderedByLengthNames.toString());
    }

    @Test
    public void orderBy_Length_ThenBy_Desc() throws Exception {
        final List<String> names = Arrays.asList("John", "Mary", "Roberto", "David", "Fernando");
        final List<String> orderedByLengthNames = asList(from(names).orderBy(length().thenBy(desc())).select());
        assertEquals("same list", "[Fernando, Roberto, David, Mary, John]", orderedByLengthNames.toString());
    }

    @Test
    public void orderBy_Length_Restricted() throws Exception {
        final List<String> names = Arrays.asList("John", "Mary", "Roberto", "David", "Fernando");
        assertEquals("same list", "[Roberto, John]", from(names).where(eq("Roberto").or(eq("John"))).orderBy(desc()).select().toString());
    }

    @Test
    public void orderBy_MultipleOf() throws Exception {
        final List<Integer> numbers = Arrays.asList(10, 12, 13, 20, 100, 1000);
        assertEquals("same list", "[10, 20, 100, 1000, 12, 13]", from(numbers).orderBy(multipleOf(10)).select().toString());
    }

    @Test(expected = FilteringException.class)
    public void orderBy_MultipleOf_Failed() throws Exception {
        final List<String> numbers = Arrays.asList("String", "Integer");
        from(numbers).orderBy(multipleOf(10)).select();
    }

}
